package com.dennytech.tac.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

public class ImageTextView extends View {

	public static final int LEFT_TOP = 0;
	public static final int LEFT_BOTTOM = 1;
	public static final int RIGHT_TOP = 2;
	public static final int RIGHT_BOTTOM = 3;

	private InnerImage leftTopImage, leftBottomImage, rightTopImage,
			rightBottomImage;
	private String text;
	private int textSize = 25;
	private int lineSpace = 10;

	private int textPaddingLeft;
	private int textPaddingRight;
	private int textPaddingTop;
	private int textPaddingBottom;
	float[] textLengths;
	int imageArea;

	private Paint paint;

	private int encode;

	public static final int ENCODE_CN = 1;

	public void setEncode(int e) {
		this.encode = e;
	}

	public ImageTextView(Context context) {
		super(context);
	}

	public ImageTextView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public ImageTextView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public void addImage(Bitmap bitmap, int postion) {
		InnerImage ii = new InnerImage(bitmap, postion);
		switch (postion) {
		case LEFT_TOP:
			leftTopImage = ii;
			break;
		case LEFT_BOTTOM:
			leftBottomImage = ii;
			break;
		case RIGHT_TOP:
			rightTopImage = ii;
			break;
		case RIGHT_BOTTOM:
			rightBottomImage = ii;
			break;

		default:
			break;
		}
	}

	public void setText(String text) {
		setText(text, 0);
	}

	public void setText(String text, int e) {
		this.text = text;
		if (text == null) {
			return;
		}
		resetVH = true;
		encode = e;
	}

	boolean resetVH = false;

	public void setTextColor(int color) {
		paint.setColor(color);
	}

	public void setTextSize(int textSize) {
		paint.setTextSize(textSize);
		this.textSize = textSize;
	}

	public void setLineSpace(int space) {
		lineSpace = space;
	}

	public void setTextPadding(int left, int top, int right, int bottom) {
		textPaddingLeft = left;
		textPaddingTop = top;
		textPaddingRight = right;
		textPaddingBottom = bottom;
	}

	// 暂时只考虑一排最多只有一张图片
	@Override
	protected void onDraw(Canvas canvas) {
		if (paint == null) {
			paint = new Paint();
			paint.setTextSize(textSize);
			paint.setColor(Color.GRAY);
			paint.setAntiAlias(true);
		}

		if (leftTopImage != null) {
			canvas.drawBitmap(leftTopImage.img, 0, 0, null);
			imageArea += leftTopImage.h * leftTopImage.w;
		}

		if (leftBottomImage != null) {
			canvas.drawBitmap(leftBottomImage.img, 0, getHeight()
					- leftBottomImage.h, null);
			imageArea += leftBottomImage.h * leftBottomImage.w;
		}

		if (rightTopImage != null) {
			canvas.drawBitmap(rightTopImage.img, getWidth() - rightTopImage.w,
					0, null);
			imageArea += rightTopImage.h * rightTopImage.w;
		}

		if (rightBottomImage != null) {
			canvas.drawBitmap(rightBottomImage.img, getWidth()
					- rightBottomImage.w, getHeight() - rightBottomImage.h,
					null);
			imageArea += rightBottomImage.h * rightBottomImage.w;
		}

		textLengths = textLength(paint, text);

		int top = textSize + textPaddingTop;
		int start = 0;
		int end = 0;

		if (leftTopImage != null) {
			while (top < leftTopImage.h + 20 && end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- leftTopImage.w - textPaddingLeft - textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText,
						leftTopImage.w + textPaddingLeft + 10, top, paint);

				top += textSize + lineSpace;
				start = end;
			}
		} else if (rightTopImage != null) {
			while (top < rightTopImage.h && end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- rightTopImage.w - textPaddingLeft - textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText, textPaddingLeft, top, paint);

				top += textSize + lineSpace;
				start = end;
			}
		}

		if (end == text.length()) {
			return;
		}

		if (leftBottomImage != null) {
			while (top < getHeight() - leftBottomImage.h && end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- textPaddingLeft - textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText, textPaddingLeft, top, paint);

				top += textSize + lineSpace;
				start = end;
			}

			while (end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- leftBottomImage.w - textPaddingLeft
						- textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText, leftBottomImage.w + textPaddingLeft,
						top, paint);

				top += textSize + lineSpace;
				start = end;
			}

		} else if (rightBottomImage != null) {
			while (top < getHeight() - rightBottomImage.h
					&& end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- textPaddingLeft - textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText, textPaddingLeft, top, paint);

				top += textSize + lineSpace;
				start = end;
			}

			while (end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- rightBottomImage.w - textPaddingLeft
						- textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText, textPaddingLeft, top, paint);

				top += textSize + lineSpace;
				start = end;
			}

		} else {
			while (end < text.length()) {
				end = computLineEnd(textLengths, start, getWidth()
						- textPaddingLeft - textPaddingRight);
				String drawText = text.substring(start, end);
				canvas.drawText(drawText, textPaddingLeft, top, paint);

				top += textSize + lineSpace;
				start = end;
			}
		}

		if (resetVH) {
			// compute lay height
			int newHeight = top + textPaddingBottom;
			LayoutParams lp = getLayoutParams();
			lp.height = newHeight;
			setLayoutParams(lp);

			resetVH = false;
		}
	}

	private float[] textLength(Paint p, String t) {
		float[] widths = new float[t.length()];
		p.getTextWidths(t, 0, t.length(), widths);
		return widths;
	}

	/**
	 * 计算规定长度限制内字符长度，返回结束的位置
	 * 
	 * @param lengths
	 * @param start
	 * @param maxLength
	 * @return
	 */
	private int computLineEnd(float[] lengths, int start, int maxLength) {
		int length = 0;
		int blank = 0;
		for (int i = start; i < lengths.length; i++) {
			char c = text.charAt(i);
			if (c == ' ') {
				blank = i;
			}

			length += lengths[i];
			if (length > maxLength) {
				if (text.charAt(i) != ' ' && encode == 0) {
					return blank;
				}
				return i - 1;
			}
		}
		return lengths.length;
	}

	static class InnerImage {
		Bitmap img;
		int w;
		int h;
		int postion;

		public InnerImage(Bitmap bm, int p) {
			postion = p;
			img = bm;
			w = bm.getWidth();
			h = bm.getHeight();
		}
	}

}
